

from core.himesis import Himesis, HimesisPostConditionPattern
import cPickle as pickle
from uuid import UUID

class HMoveOneInputIndirectMatchDiffRulesRHS(HimesisPostConditionPattern):
    def __init__(self):
        """
        Creates the himesis graph representing the AToM3 model HMoveOneInputIndirectMatchDiffRulesRHS.
        """
        # Flag this instance as compiled now
        self.is_compiled = True
        
        super(HMoveOneInputIndirectMatchDiffRulesRHS, self).__init__(name='HMoveOneInputIndirectMatchDiffRulesRHS', num_nodes=10, edges=[])
        
        # Add the edges
        self.add_edges([(1, 6), (6, 3), (2, 7), (7, 4), (1, 8), (8, 5), (2, 9), (9, 5), (0, 3), (5, 0)])
        # Set the graph attributes
        self["mm__"] = pickle.loads("""(lp1
S'MT_post__SimpleDSLTransMM'
p2
aS'MoTifRule'
p3
a.""")
        self["MT_action__"] = pickle.loads("""V#===============================================================================\u000a# This code is executed after the rule has been applied.\u000a# You can access a node labelled n matched by this rule by: PostNode('n').\u000a# To access attribute x of node n, use: PostNode('n')['x'].\u000a#===============================================================================\u000a\u000apass\u000a
p1
.""")
        self["name"] = """"""
        self["GUID__"] = UUID('7ddaed1c-a9a4-4473-951b-41a86bde0a04')
        
        # Set the node attributes
        self.vs[0]["MT_label__"] = """13"""
        self.vs[0]["mm__"] = """MT_post__indirectLink_S"""
        self.vs[0]["GUID__"] = UUID('c6921962-5fd4-46e7-97de-a858f24cd39e')
        self.vs[1]["MT_label__"] = """1"""
        self.vs[1]["mm__"] = """MT_post__MatchModel"""
        self.vs[1]["GUID__"] = UUID('74259d58-17a1-45e8-96c9-a7da297311b4')
        self.vs[2]["MT_label__"] = """2"""
        self.vs[2]["mm__"] = """MT_post__MatchModel"""
        self.vs[2]["GUID__"] = UUID('a55b4dfc-0d47-4405-a8c3-aa473cad170b')
        self.vs[3]["MT_pivotOut__"] = """element1"""
        self.vs[3]["MT_label__"] = """3"""
        self.vs[3]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[3]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[3]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[3]["GUID__"] = UUID('7fe3f3e4-68c9-41c0-8271-215b6336fdda')
        self.vs[4]["MT_pivotOut__"] = """element2"""
        self.vs[4]["MT_label__"] = """4"""
        self.vs[4]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[4]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[4]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[4]["GUID__"] = UUID('40f0b734-ff49-40ce-aa71-a95408baba8b')
        self.vs[5]["MT_label__"] = """5"""
        self.vs[5]["MT_post__name"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["mm__"] = """MT_post__MetaModelElement_S"""
        self.vs[5]["MT_post__classtype"] = """
#===============================================================================
# You can access the value of the current node's attribute value by: attr_value.
# If the current node shall be created you MUST initialize it here!
# You can access a node labelled n by: PreNode('n').
# To access attribute x of node n, use: PreNode('n')['x'].
# Note that the attribute values are those before the match is rewritten.
# The order in which this code is executed depends on the label value
# of the encapsulating node.
# The given action must return the new value of the attribute.
#===============================================================================

return attr_value
"""
        self.vs[5]["GUID__"] = UUID('8f5671cf-e4a7-4ca8-9d97-122af4ab95e2')
        self.vs[6]["MT_label__"] = """6"""
        self.vs[6]["mm__"] = """MT_post__match_contains"""
        self.vs[6]["GUID__"] = UUID('5728f5fb-becd-4c94-a1f3-451bf148fbd8')
        self.vs[7]["MT_label__"] = """7"""
        self.vs[7]["mm__"] = """MT_post__match_contains"""
        self.vs[7]["GUID__"] = UUID('b40df7fe-9041-43de-9cec-a5f600b1e2dd')
        self.vs[8]["MT_label__"] = """12"""
        self.vs[8]["mm__"] = """MT_post__match_contains"""
        self.vs[8]["GUID__"] = UUID('73b79e04-e4e1-441b-8816-b880992f7a23')
        self.vs[9]["MT_label__"] = """8"""
        self.vs[9]["mm__"] = """MT_post__match_contains"""
        self.vs[9]["GUID__"] = UUID('c47fb743-6917-48ba-8a6c-7d9932f71e7b')

        from HMoveOneInputIndirectMatchDiffRulesLHS import HMoveOneInputIndirectMatchDiffRulesLHS
        self.pre = HMoveOneInputIndirectMatchDiffRulesLHS()
    
    def action(self, PostNode, graph):
        """
            Executable constraint code. 
            @param PostNode: Function taking an integer as parameter
                             and returns the node corresponding to that label.
        """
        #===============================================================================
        # This code is executed after the rule has been applied.
        # You can access a node labelled n matched by this rule by: PostNode('n').
        # To access attribute x of node n, use: PostNode('n')['x'].
        #===============================================================================
        
        pass

    def execute(self, packet, match):
        """
            Transforms the current match of the packet according to the rule %s.
            Pivots are also assigned, if any.
            @param packet: The input packet.
            @param match: The match to rewrite.
        """
        graph = packet.graph
        
        # Build a dictionary {label: node index} mapping each label of the pattern to a node in the graph to rewrite.
        # Because of the uniqueness property of labels in a rule, we can store all LHS labels
        # and subsequently add the labels corresponding to the nodes to be created.
        labels = match.copy()
        
        #===============================================================================
        # Update attribute values
        #===============================================================================
        
        #===============================================================================
        # Create new nodes
        #===============================================================================
        # match_contains12
        new_node = graph.add_node()
        labels['12'] = new_node
        graph.vs[new_node][Himesis.Constants.META_MODEL] = 'match_contains'
        # indirectLink_S13
        new_node = graph.add_node()
        labels['13'] = new_node
        graph.vs[new_node][Himesis.Constants.META_MODEL] = 'indirectLink_S'
        
        #===============================================================================
        # Create new edges
        #===============================================================================
        # MatchModel1 -> match_contains12
        graph.add_edges((labels['1'], labels['12']))
        # match_contains12 -> MetaModelElement_S5
        graph.add_edges((labels['12'], labels['5']))
        # indirectLink_S13 -> MetaModelElement_S3
        graph.add_edges((labels['13'], labels['3']))
        # MetaModelElement_S5 -> indirectLink_S13
        graph.add_edges((labels['5'], labels['13']))
        
        #===============================================================================
        # Set the output pivots
        #===============================================================================
        # MetaModelElement_S3
        packet.global_pivots['element1'] = graph.vs[labels['3']][Himesis.Constants.GUID]
        # MetaModelElement_S4
        packet.global_pivots['element2'] = graph.vs[labels['4']][Himesis.Constants.GUID]
        
        #===============================================================================
        # Perform the post-action
        #===============================================================================
        try:
            self.action(lambda i: graph.vs[labels[i]], graph)
        except Exception, e:
            raise Exception('An error has occurred while applying the post-action', e)
        #===============================================================================
        # Finally, delete nodes (this will automatically delete the adjacent edges)
        #===============================================================================
        # MT_pre__indirectLink_S9
        graph.delete_nodes([labels["9"]])
    
